package com.wxq.shedule.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wxq.shedule.domain.Task;
import com.wxq.shedule.service.TaskService;
import jodd.util.StringUtil;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.TimeUnit;

@Component
public class TaskExecute implements Runnable{
    @Autowired
    private TaskService taskService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    ValueOperations valueOperations;


    private String taskId;



    @Override
    public void run() {
        Task task = taskService.getOne(new QueryWrapper<Task>().lambda()
                .le(Task::getStartTimePlan,new Date()));
        System.err.println(task.getId().toString());
        valueOperations.set(task.getId().toString(),"ww",40, TimeUnit.SECONDS);
        // 用作停止线程
        while (true) {
            if (!StringUtil.isEmpty(valueOperations.get(task.getId().toString()).toString())) {
                System.err.println("111111");
                break;
            }
        }
        redisTemplate.delete(task.getId().toString());

    }
}
